package com.cyou.nad.datastore;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.Date;

/**
 * 列的数据属性
 * 
 * @author lzm 
 */
public enum ColType {
	STRING, INTEGER, LONG, FLOAT, DOUBLE, DATE, TIMESTAMP, OBJECT, WRONG;
	public static Object getResultSetValue(ResultSet rs, int columnIndex, ColType type) throws SQLException {
		switch (type) {
		case STRING:
			return rs.getString(columnIndex);
		case LONG:
			return rs.getLong(columnIndex);
		case INTEGER:
			return rs.getInt(columnIndex);
		case FLOAT:
			return rs.getFloat(columnIndex);
		case DOUBLE:
			return rs.getDouble(columnIndex);
		case TIMESTAMP:
			return rs.getTimestamp(columnIndex);
		case DATE:
			return rs.getDate(columnIndex);
		case OBJECT:
			return rs.getObject(columnIndex);
		case WRONG:
			throw new UnsupportedOperationException("datastore  不支撑当前的数据类型");
		default:
			return rs.getObject(columnIndex);

		}
	}

	public static void main(String[] args) {
		System.out.println(STRING);
		Object obj = "";
		System.out.println(ColType.STRING.match(obj));
		System.out.println(ColType.LONG.match(obj));

		obj = 1;
		System.out.println(ColType.LONG.match(obj));
		System.out.println(ColType.DOUBLE.match(obj));
	}

	public static ColType parse(Object obj) {
		if (obj instanceof String)
			return STRING;
		if (obj instanceof Integer)
			return INTEGER;
		if (obj instanceof Long)
			return LONG;
		if (obj instanceof Float)
			return FLOAT;
		if (obj instanceof Double)
			return DOUBLE;
		if (obj instanceof Timestamp)
			return TIMESTAMP;
		if (obj instanceof Date)
			return DATE;
		return OBJECT;
	}

	public static ColType parseSqlType(int sqlType) {
		switch (sqlType) {
		case Types.BIT:
		case Types.TINYINT:
		case Types.SMALLINT:
		case Types.INTEGER:
			return ColType.INTEGER;
		case Types.BIGINT:
			return ColType.LONG;

		case Types.FLOAT:
		case Types.REAL:
			return ColType.FLOAT;

		case Types.DOUBLE:
		case Types.NUMERIC:
		case Types.DECIMAL:
			return ColType.DOUBLE;

		case Types.CHAR:
		case Types.VARCHAR: // '\001'
		case Types.LONGVARCHAR: // '\f'
		case Types.VARBINARY:
			return ColType.STRING;

		case Types.DATE: // '['
		case Types.TIME: // '\\'
			return ColType.DATE;
		case Types.TIMESTAMP: // ']'
			return ColType.TIMESTAMP;
		case -4:

		default:
			System.out.println(sqlType);
			return WRONG;
		}
	}
	
	public boolean isNumberType(){
		if(this.equals(INTEGER)||this.equals(LONG)||this.equals(FLOAT)||this.equals(DOUBLE)){
			return true;
		}else{
			return false;
		}
	}

	public boolean match(Object obj) {
		switch (this) {
		case OBJECT:
			return true;
		case STRING:
			if (obj instanceof String)
				return true;
			else
				return false;

		case INTEGER:
			if (obj instanceof Integer)
				return true;
			else
				return false;
		case LONG:
			if (obj instanceof Long)
				return true;
			else
				return false;
		case FLOAT:
			if (obj instanceof Float)
				return true;
			else
				return false;
		case DOUBLE:
			if (obj instanceof Double)
				return true;
			else
				return false;
		case TIMESTAMP:
			if (obj instanceof Timestamp)
				return true;
			else
				return false;
		case DATE:
			if (obj instanceof Date)
				return true;
			else
				return false;
		default:
			return false;
		}
	}

}
